﻿using GBT_Tool_v4._0;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ExportMealToExcel
{
    public partial class ModifyPassword : Form
    {

        private Logger logger = LogManager.GetCurrentClassLogger();
        bool isLogin = false;
        Win32 w32 = new Win32();
        HttpRequest hr = new HttpRequest();
        Encryption ency = new Encryption();

        bool isModify = false;

        public ModifyPassword()
        {
            InitializeComponent();
        }

        public string message { get; set; }

        private void ModifyPassword_Load(object sender, EventArgs e)
        {

        }

        private void UserModifyPassword()
        {
            try
            {
                JObject param = new JObject();

                if (txtMobile.InvokeRequired)
                {
                    Action<object> actionDelegate = (o) =>
                    {
                        param["mobile"] = txtMobile.Text;
                        param["oldPassword"] = Encryption.Get_MD5(txtOldPassword.Text);
                        param["password"] = Encryption.Get_MD5(txtNewPassword.Text);

                        lblMessage.ForeColor = Color.Black;
                        lblMessage.Text = "就绪";
                    };
                    txtMobile.BeginInvoke(actionDelegate, "");
                }
                //海关
                //param["districtID"] = 1;
                //税务局
                param["districtID"] = 17;
                logger.Info(param);

                string requestJson = JsonConvert.SerializeObject(param);
                string resultJson = hr.GBTPost(Program.host_url + "gbt/UserVerify/ModifyPassword", requestJson);
                JObject joResult = JObject.Parse(resultJson);
                JObject joResultRoot = (JObject)joResult["root"];
                logger.Info(joResultRoot);
                
                int line = (int)joResultRoot["data"];
                if (lblMessage.InvokeRequired)
                {
                    Action<object> actionDelegate = (o) =>
                    {
                        bool state = (bool)joResultRoot["state"];
                        
                        bool isEmpty = true;
                        string message = "";
                        if (txtMobile.Text == "")
                        {
                            message += "手机号不能为空\n";
                            isEmpty = false;
                        }
                        //if (txtOldPassword.Text == "")
                        //{
                        //    message += "原密码不能为空\n";
                        //    isEmpty = false;
                        //}
                        if (txtNewPassword.Text == "")
                        {
                            message += "新密码不能为空\n";
                            isEmpty = false;
                        }
                        if (txtConfirmNewPassword.Text == "")
                        {
                            message += "确认新密码不能为空\n";
                            isEmpty = false;
                        }
                        if (txtNewPassword.Text != txtConfirmNewPassword.Text)
                        {
                            message += "新密码、确认新密码输入不一致，请重新输入\n";
                            isEmpty = false;
                        }
                        if (!isEmpty)
                        { 
                            DialogResult dr = MessageBox.Show(message, "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            if (dr == DialogResult.OK)
                            {
                                if (state)
                                {
                                    lblMessage.ForeColor = Color.Black;
                                }
                                else
                                {
                                    lblMessage.ForeColor = Color.Red;
                                }
                            }
                            this.message = (string)joResultRoot["message"];
                            lblMessage.Text = (string)joResultRoot["message"];
                            this.Refresh();
                            return;
                        }

                        if (state)
                        {
                            lblMessage.ForeColor = Color.Black;
                        }
                        else
                        {
                            lblMessage.ForeColor = Color.Red;
                        }
                        this.message = (string)joResultRoot["message"];
                        lblMessage.Text = (string)joResultRoot["message"];
                        this.Refresh();

                    };
                    lblMessage.BeginInvoke(actionDelegate, "");
                }
                //Thread.Sleep(1000);
                if (line > 0)
                {
                    //修改成功则关闭登陆页面
                    isModify = true;
                    DialogResult dr = MessageBox.Show((string)joResultRoot["message"], "温馨提示");
                    if (dr == DialogResult.OK)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnModifyPassword_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(UserModifyPassword);
            t.Start();
        }
    }
}
